import pathToRegexp from 'path-to-regexp/index';
import {getDutyList,getDutyMM,getDutyYY,postDuty,getDutyTXL} from 'services/app';
export default {
    namespace: 'duty',
    state: {
        bodyWidth:document.body.clientWidth,
    },
    subscriptions: {
        setupHistory ({ dispatch, history }) {
            history.listen((location) => {
                const match = pathToRegexp('/home').exec(location.pathname);
                dispatch({type:'getDutyMM',payload:{date:new Date().format('yyyy-MM-dd HH:mm:ss')}});
                // if(location.pathname == '/dutymanagement'){
                // dispatch({type:'getDutyYY',payload:{date:new Date().format('yyyy-MM-dd HH:mm:ss')}});
                // }
            });
        },
        setup ({ dispatch }) {},
    },
    effects: {
        //获取日程月
        *getDutyMM ({ payload }, { call,put }){
            const {data} = yield call(getDutyMM, payload);
            yield put({ type: 'save', payload: {getDutyMMData:data} });
        },
        //获取日程年
        *getDutyYY ({ payload }, { call,put }){
            const {data} = yield call(getDutyYY, payload);
            yield put({ type: 'save', payload: {getDutyYYData:data} });
        },
        //获取日程详情
        *getDutyList ({ payload }, { call,put }){
            // const {data} = yield call(getDutyList, {
            //     date:payload.timeDate,
            // });
            const dataTow = yield call(getDutyTXL);
            yield put({ type: 'save', payload: {getDutyListData:[],TXL:dataTow.data,view:true,timeStart:payload.timeStart} });
        },
        //添加日程
        *postDuty ({ payload }, { call,put }){
            console.log(payload);
            const data = yield call(postDuty, payload);
            if(data.code == 0){
                const {data} = yield call(getDutyMM, {date: new Date().format('yyyy-MM-dd HH:mm:ss')});
                yield put({ type: 'save', payload: {getDutyMMData:data,view:false} });
            }
        },
        //输入等于输入
        *inputOut ({ payload }, { put }){
            yield put({ type: 'save', payload: payload });
        },
    },
    reducers: {
        save (state, { payload }) {
            return { ...state, ...payload };
        },
    },
};
